package com.pdstudio.weather;

import java.util.List;
import com.pdstudio.apapter.WarningListAdapter;
import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.Warning;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WarningListActivity extends Activity implements EventHandler{
	
	private WarningListAdapter warningListAdatper;
	private ListView lvWarning;
	private List<Warning> mWarnings;
	private Handler handler =null;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;
	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_warning_list);
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();
		
		lvWarning=(ListView) findViewById(R.id.warning_list);
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_warning_list);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));


		customLiveIndexTitleView = findViewById(R.id.title_warning);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("预警信号");

		ivBack = (ImageView) customLiveIndexTitleView
				.findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WarningListActivity.this.finish();
			}

		});

		
		newRunnable();
	}
	
	private void newRunnable() {
		// TODO Auto-generated method stub

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					Message msg;
					WeatherHttpMethod http = new WeatherHttpMethod(
							WarningListActivity.this);
					mWarnings = http.GetWarning(mSpUtil.getAuthCode(),
							mApplication.getCurSimpleCity().cityCode);
					msg = handler.obtainMessage(1, mWarnings);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {					
					if (msg.what == 1) {
						Toast.makeText(getApplicationContext(), "加载数据中", 0)
								.show();
						if(mWarnings.size()==0){
							Warning w = new Warning();
							w.postTime="无";
							w.tip="当前无预警信息";
							w.warningAddress="无";
							w.warningCode="无";
							w.warningType="无";		
							mWarnings.add(w);
						}
						setWarnings(mWarnings);
					}
				}

			};
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setWarnings(List<Warning> warnings) {
		try {
			warningListAdatper = new WarningListAdapter(this, warnings);
			lvWarning.setAdapter(warningListAdatper);			
		} catch (Exception e) {
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.warning_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub
		
	}
}
